﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsAppweek2
{
    public partial class JobDetailsForm : Form
    {
        private SqlConnection con;
        SqlDataAdapter adapter;
        DataSet jobDetailsDataSet;
        BindingSource bindingSource;
        public JobDetailsForm()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void label21_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if ((txtCarNo.Text == "") || (txtCarNo.Text == null))
            {
                MessageBox.Show("wrong");
                txtCarNo.Focus();
            }
        }



        private void txtWorkId_Click(object sender, EventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=LAPTOP-ANARUPVH;Initial Catalog=CMSDatabase;Integrated Security=True;Encrypt=True;TrustServerCertificate=True");
            adapter = new SqlDataAdapter("select *from tblJobDetails", con);

            jobDetailsDataSet = new DataSet();

            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(jobDetailsDataSet, "tblJobDetails");

            bindingSource = new BindingSource { DataSource = jobDetailsDataSet.Tables["tblJobDetails"] };

            foreach (Control control in this.Controls) {
                if (control is TextBox txt && txt.Name.StartsWith("txt")) {
                    txt.DataBindings.Clear();
                    String columnName = txt.Name.Substring(3);
                    txt.DataBindings.Add("Text",bindingSource,columnName);
                }
            }
            updatePositionLabel();
        }

        private void updatePositionLabel()
        {
            if (bindingSource.Count > 0)
            {
                textPosition.Text = $"{bindingSource.Position + 1} of {bindingSource.Count}";

            }
            else
            {
                textPosition.Text = "0 of 0";
                }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_BindingContextChanged(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position < bindingSource.Count - 1)
            {
                bindingSource.MoveNext();
                updatePositionLabel();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position > 0)
            {
                bindingSource.MovePrevious();
                updatePositionLabel();
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            bindingSource.MoveFirst();
            updatePositionLabel();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            bindingSource.MoveLast();
            updatePositionLabel();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtCarNo.Text.Length < 6)
                {
                    MessageBox.Show("wrong number");
                    txtCarNo.Focus();
                    return;
                }
                if (Convert.ToInt32(txtWorkerId.Text) < 1)
                {
                    MessageBox.Show("wrong number");
                    txtWorkerId.Focus();
                    return;

                }
                if (Convert.ToDateTime(dtJobDate.Text) > DateTime.Today)
                {
                    MessageBox.Show("wrong date");
                    dtJobDate.Focus();
                    return;
                }
                MessageBox.Show("Selected Date: " + dtJobDate.Value.ToString());
                try
                {

                    bindingSource.EndEdit();
                    adapter.Update(jobDetailsDataSet, "tblJobDetails");
                    MessageBox.Show("success");
                }
                catch (Exception ex) {

                    MessageBox.Show("wrong"+ex.Message);
                }



            }
            catch (Exception ex) {
                MessageBox.Show("WRONG!!!");
            }




            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (bindingSource == null) {
                bindingSource = new BindingSource();

            }
            if (bindingSource.DataSource == null) {
                MessageBox.Show("DATA IS EMPTY");
                return;
            }
            bindingSource.DataSource = jobDetailsDataSet.Tables["tblJobDetails"];
            bindingSource.AddNew();
            updatePositionLabel();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bindingSource.Count > 0) {
                bindingSource.RemoveCurrent();
                adapter.Update(jobDetailsDataSet, "tblJobDetails");
                MessageBox.Show("success");
                updatePositionLabel();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bindingSource.CancelEdit();
            updatePositionLabel();
        }

        private void btnCancelAll_Click(object sender, EventArgs e)
        {
            jobDetailsDataSet.RejectChanges();
            updatePositionLabel();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtJobDate_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
